<?php
 namespace App\Controllers;
 class Dcmember extends BaseController
 {
 public function index()
 {
     // connect to the model
 $dc = new \App\Models\Dc();
 // retrieve all the records
 $records = $dc->findAll();
 $table = new \CodeIgniter\View\Table();
 
 $headings = $dc->fields;
 
 $displayHeadings = ["Name","Image","Link"];
 $table->setHeading(array_map('ucfirst', $displayHeadings));

foreach ($records as $record) {
    $DetailsLink = anchor("dcmember/showme/$record->id", "DcDetails");
    $table->addRow($record->name, "<img src=/image/".$record->image.">",$DetailsLink);
}

$template = [
'table_open' => '<table cellpadding="5px">',
'cell_start' => '<td style="border: 1px solid #dddddd;">',
'row_alt_start' => '<tr style="background-color:#dddddd">',
];
$table->setTemplate($template);

$fields = [
 'title' => 'Dcmember List',
 'heading' => 'Dcmember List',
 'footer' => 'Copyright Shengyan Xie'
 ];

    $parser = \Config\Services::parser();
    return $parser->setData($fields)
                ->render('templates\top') .
           $table->generate() .
                $parser->setData($fields)
                ->render('templates\bottom');

    
 }
 public function showme($id)
 {
     
 $dc = new \App\Models\Dc();
 $table = new \CodeIgniter\View\Table();
 $record = $dc->find($id);
 
$table->addRow('id:',$record['id']);
$table->addRow('name:',$record['name']);
$table->addRow('birth:',$record['birth']);
$table->addRow('height:',$record['height']);
$table->addRow('weight:',$record['weight']);
$table->addRow('country:',$record['country']);
$table->addRow('link:',$record['link']);
$table->addRow('image:',"<img src=/image/".$record['image'].">");
 
$template = [
'table_open' => '<table cellpadding="5px">',
'cell_start' => '<td style="border: 1px solid #dddddd;">',
'row_alt_start' => '<tr style="background-color:#dddddd">',
];
$table->setTemplate($template);

$fields = [
 'title' => 'DcMember introduction',
 'heading' => 'DcMember introduction',
 'footer' => 'Shengyang Xie',
 'edit'=>'edit'
 ];

 $parser = \Config\Services::parser();

return $parser->setData($fields)
 ->render('templates\top') .
 $parser->renderString(anchor("dcmember/edit/" . $record['id'],"Edit")) .
 $table->generate() .
 $parser->setData($fields)
 ->render('templates\bottom');

 }
public function edit($id = NULL) {
        $mc = new \App\Models\Dc();
        $record = $dc->find($id);
        $fields = [
            'title' => 'Edit a Dcmember',
            'heading' => 'Edit a Dcmember',
            'footer' => 'Copyright Shengyang Xie'
            ];
        $parser = \Config\Services::parser();
        return $parser->setData($fields)
                        ->render('templates\top') .
                        $parser->setData($record)
                        ->render('editdc') .
                        $parser->setData($fields)
                        ->render('templates\bottom');
    }
    public function handle() { 
    $dc = new \App\Models\Dc();
    helper(['form', 'url']);
    $record = $dc->find($this->request->getPost('id'));
    if ($this->validate($dc->validationRules)) {
    $dc->save([
 'id' => $this->request->getPost('id'),
 'name' => $this->request->getPost('name'),
 'birth' => $this->request->getPost('birth'),
 'height' => $this->request->getPost('height'),
 'weight' => $this->request->getPost('weight'),
 'country' => $this->request->getPost('country'),
 'link' => $this->request->getPost('link'),
 'image' => $this->request->getPost('image'),
 ]);
 }
 $fields = [
 'title' => 'Edit a dcmember',
 'heading' => 'Edit a dcmember',
 'footer' => 'Copyright Shengyang Xie'
 ];
 $parser = \Config\Services::parser();

 $returnView = $parser->setData($fields)->render('templates\top');
 

 if (!$this->validate($dc->validationRules)) {
 $returnView .= $parser->renderString($this->validator->listErrors());
 $returnView .= $parser->setData($this->request->getPost())->render('editdc');
 } else {
 $returnView .= $parser->renderString("<p>Success!</p>");
 $returnView .= $parser->setData($record)->render('editdc');
 }
 

 $returnView .= $parser->setData($fields)->render('templates\bottom');
 return $returnView;
}
 }